package org.hcyspartnoc.luoxin.service.searchtopic.impl;

import org.hcyspartnoc.luoxin.common.core.context.BaseContextHolder;
import org.hcyspartnoc.luoxin.common.exception.searchtopic.QuestionBankException;
import org.hcyspartnoc.luoxin.mapper.searchtopic.QuestionBankMapper;
import org.hcyspartnoc.luoxin.pojo.dto.searchtopic.QuestionBankDto;
import org.hcyspartnoc.luoxin.pojo.po.searchtopic.QuestionBankPo;
import org.hcyspartnoc.luoxin.pojo.vo.searchtopic.QuestionBankVO;
import org.hcyspartnoc.luoxin.service.searchtopic.QuestionBankService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Service
public class QuestionBankServiceImpl implements QuestionBankService {
    @Resource
    private QuestionBankMapper questionBankMapper;

    @Override
    public void updateQuestionBank(QuestionBankDto questionBankDto) throws QuestionBankException {
        if (questionBankDto==null){
            throw new QuestionBankException(QuestionBankException.REQUEST_FAILED);
        }
        // 题库名字不能包含特殊字符
        String validPattern = "[`~!@#$%^&*()+=|{}':;',\\\\[\\\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
        Matcher matcher = Pattern.compile(validPattern).matcher(questionBankDto.getName());
        if (matcher.find()) {
            throw new QuestionBankException(QuestionBankException.BANK_NAME_ERROR);
        }
        QuestionBankPo questionBankPo=new QuestionBankPo();
        BeanUtils.copyProperties(questionBankDto,questionBankPo);
        questionBankPo.setUpdatedTime(LocalDateTime.now());
        questionBankPo.setCreatedTime(LocalDateTime.now());
        questionBankPo.setUserId(BaseContextHolder.getContext().getBaseCurrentUser().getId());
        questionBankMapper.updateQuestionBank(questionBankPo);
    }
    @Override
    public List<QuestionBankVO> getQuestionBank() throws QuestionBankException {
        Integer userId= BaseContextHolder.getContext().getBaseCurrentUser().getId();
        List<QuestionBankVO> list=questionBankMapper.selectQuestionBank(userId);
        if (list==null){
            throw new QuestionBankException(QuestionBankException.BANK_EMPTY);
        }
        return list;
    }

    @Override
    public void createQuestionBank(QuestionBankDto questionBankDto) throws QuestionBankException {
        //检验创建的题库名字是否为空
        if (questionBankDto.getName()==null){
            throw new QuestionBankException(QuestionBankException.BANK_NAME_EMPTY);
        }
        // 题库名字不能包含特殊字符
        String validPattern = "[`~!@#$%^&*()+=|{}':;',\\\\[\\\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
        Matcher matcher = Pattern.compile(validPattern).matcher(questionBankDto.getName());
        if (matcher.find()) {
           throw new QuestionBankException(QuestionBankException.BANK_NAME_ERROR);
        }
        QuestionBankPo questionBankPo=new QuestionBankPo();
        BeanUtils.copyProperties(questionBankDto,questionBankPo);
        questionBankPo.setUpdatedTime(LocalDateTime.now());
        questionBankPo.setCreatedTime(LocalDateTime.now());
        questionBankPo.setUserId(BaseContextHolder.getContext().getBaseCurrentUser().getId());
        Integer i=questionBankMapper.insertQuestionBank(questionBankPo);
        if (i==0){
            throw new QuestionBankException(QuestionBankException.Bank_FORMAT_ERROR);
        }
    }
    @Override
    public void deleteQuestionBank(Long id) throws QuestionBankException {
        if (id<=0){
            throw new QuestionBankException(QuestionBankException.BANK_ID_ERROR);
        }
        Integer userId= BaseContextHolder.getContext().getBaseCurrentUser().getId();
        questionBankMapper.deleteQuestionBank(id);
    }
    @Override
    public void deleteQuestionBanks(List<Integer> ids) throws QuestionBankException {
        for (Integer id:ids) {
            if (id<=0){
                throw new QuestionBankException(QuestionBankException.BANK_ID_ERROR);
            }
        }
        Integer userId= BaseContextHolder.getContext().getBaseCurrentUser().getId();
        questionBankMapper.deleteQuestionBanks(ids);
    }

}

